package com.muyu.marketing.bargain.controller;

import com.muyu.common.core.domain.Result;
import com.muyu.common.core.exception.base.BaseException;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.web.page.DataPageResp;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.annotation.RequiresPermissions;
import com.muyu.marketing.bargain.domain.BargainProduct;
import com.muyu.marketing.bargain.domain.req.BargainProductReq;
import com.muyu.marketing.bargain.domain.resp.BargainProductResp;
import com.muyu.marketing.bargain.service.BargainProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 砍价商品Controller
 * 
 * @author muyu
 * @date 2025-01-27
 */
@Slf4j
@RestController
@RequestMapping("/marketing/bargain/product")
@CrossOrigin(origins = "*", allowedHeaders = "*", methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT, RequestMethod.DELETE, RequestMethod.OPTIONS})
@Tag(name = "砍价商品管理", description = "砍价商品相关接口")
public class BargainProductController extends BaseController {
    
    @Autowired
    private BargainProductService bargainProductService;
    
    /**
     * 查询砍价商品列表
     */
    @GetMapping("/list")
    @Operation(summary = "查询砍价商品列表", description = "分页查询砍价商品列表")
    public Result<DataPageResp<BargainProductResp>> list(BargainProduct bargainProduct) {
        try {
            List<BargainProductResp> list = bargainProductService.selectBargainProductList(bargainProduct);
            return Result.success(new DataPageResp<>(0, list));
        } catch (Exception e) {
            log.error("查询砍价商品列表失败", e);
            return Result.error("查询砍价商品列表失败: " + e.getMessage());
        }
    }
    
    /**
     * 导出砍价商品列表
     */
    @PostMapping("/export")
    @Operation(summary = "导出砍价商品列表", description = "导出砍价商品列表到Excel")
    @RequiresPermissions("marketing:bargain:export")
    @Log(title = "砍价商品", businessType = BusinessType.EXPORT)
    public void export(BargainProduct bargainProduct) {
        List<BargainProductResp> list = bargainProductService.selectBargainProductList(bargainProduct);

    }
    
    /**
     * 获取砍价商品详细信息
     */
    @GetMapping(value = "/{id}")
    @Operation(summary = "获取砍价商品详细信息", description = "根据ID获取砍价商品详细信息")
    public Result<BargainProductResp> getInfo(@Parameter(description = "砍价商品ID", required = true) @PathVariable("id") Long id) {
        try {
            BargainProductResp bargainProduct = bargainProductService.selectBargainProductById(id);
            return Result.success(bargainProduct);
        } catch (Exception e) {
            log.error("获取砍价商品详细信息失败", e);
            return Result.error("获取砍价商品详细信息失败: " + e.getMessage());
        }
    }
    
    /**
     * 新增砍价商品
     */
    @PostMapping
    @Operation(summary = "新增砍价商品", description = "创建新的砍价商品")
    @RequiresPermissions("marketing:bargain:add")
    @Log(title = "砍价商品", businessType = BusinessType.INSERT)
    public Result<Void> add(@Parameter(description = "砍价商品信息", required = true) @RequestBody @Validated BargainProductReq bargainProductReq) {
        try {
            int result = bargainProductService.insertBargainProduct(bargainProductReq);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.error("新增砍价商品失败");
            }
        } catch (Exception e) {
            log.error("新增砍价商品失败", e);
            return  Result.error("新增砍价商品失败: " + e.getMessage());
        }
    }
    
    /**
     * 修改砍价商品
     */
    @PutMapping
    @Operation(summary = "修改砍价商品", description = "更新砍价商品信息")
    @RequiresPermissions("marketing:bargain:edit")
    @Log(title = "砍价商品", businessType = BusinessType.UPDATE)
    public Result<Void> edit(@Parameter(description = "砍价商品信息", required = true) @RequestBody @Validated BargainProductReq bargainProductReq) {
        try {
            int result = bargainProductService.updateBargainProduct(bargainProductReq);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.error("修改砍价商品失败");
            }
        } catch (Exception e) {
            log.error("修改砍价商品失败", e);
            return Result.error("修改砍价商品失败: " + e.getMessage());
        }
    }
    
    /**
     * 删除砍价商品
     */
    @DeleteMapping("/{ids}")
    @Operation(summary = "删除砍价商品", description = "批量删除砍价商品")
    @RequiresPermissions("marketing:bargain:remove")
    @Log(title = "砍价商品", businessType = BusinessType.DELETE)
    public Result<Void> remove(@Parameter(description = "砍价商品ID数组", required = true) @PathVariable Long[] ids) {
        try {
            int result = bargainProductService.deleteBargainProductByIds(ids);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.error("删除砍价商品失败");
            }
        } catch (Exception e) {
            log.error("删除砍价商品失败", e);
            return Result.error("删除砍价商品失败: " + e.getMessage());
        }
    }
    
    /**
     * 开启砍价商品
     */
    @PutMapping("/enable/{id}")
    @Operation(summary = "开启砍价商品", description = "开启指定的砍价商品")
    @RequiresPermissions("marketing:bargain:edit")
    @Log(title = "砍价商品", businessType = BusinessType.UPDATE)
    public Result<Void> enable(@Parameter(description = "砍价商品ID", required = true) @PathVariable("id") Long id) {
        try {
            int result = bargainProductService.enableBargainProduct(id);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.error("开启砍价商品失败");
            }
        } catch (Exception e) {
            log.error("开启砍价商品失败", e);
            return  Result.error("开启砍价商品失败: " + e.getMessage());
        }
    }
    
    /**
     * 关闭砍价商品
     */
    @PutMapping("/disable/{id}")
    @Operation(summary = "关闭砍价商品", description = "关闭指定的砍价商品")
    @RequiresPermissions("marketing:bargain:edit")
    @Log(title = "砍价商品", businessType = BusinessType.UPDATE)
    public Result<Void> disable(@Parameter(description = "砍价商品ID", required = true) @PathVariable("id") Long id) {
        try {
            int result = bargainProductService.disableBargainProduct(id);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.error("关闭砍价商品失败");
            }
        } catch (Exception e) {
            log.error("关闭砍价商品失败", e);
            return  Result.error("关闭砍价商品失败: " + e.getMessage());
        }
    }
    
    /**
     * 复制砍价商品
     */
    @PostMapping("/copy/{id}")
    @Operation(summary = "复制砍价商品", description = "复制指定的砍价商品")
    @RequiresPermissions("marketing:bargain:add")
    @Log(title = "砍价商品", businessType = BusinessType.INSERT)
    public Result<Void> copy(@Parameter(description = "砍价商品ID", required = true) @PathVariable("id") Long id) {
        try {
            int result = bargainProductService.copyBargainProduct(id);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.error("复制砍价商品失败");
            }
        } catch (Exception e) {
            log.error("复制砍价商品失败", e);
            return  Result.error("复制砍价商品失败: " + e.getMessage());
        }
    }
    
    /**
     * 查询进行中的砍价商品列表
     */
    @GetMapping("/active")
    @Operation(summary = "查询进行中的砍价商品列表", description = "查询状态为进行中的砍价商品列表")
    public Result<List<BargainProductResp>> getActiveList() {
        try {
            List<BargainProductResp> list = bargainProductService.selectActiveBargainProductList();
            return Result.success(list);
        } catch (Exception e) {
            log.error("查询进行中的砍价商品列表失败", e);
            return Result.error("查询进行中的砍价商品列表失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据状态查询砍价商品列表
     */
    @GetMapping("/status/{status}")
    @Operation(summary = "根据状态查询砍价商品列表", description = "根据状态查询砍价商品列表")
    public Result<List<BargainProductResp>> getListByStatus(@Parameter(description = "状态", required = true) @PathVariable("status") Integer status) {
        try {
            List<BargainProductResp> list = bargainProductService.selectBargainProductListByStatus(status);
            return Result.success(list);
        } catch (Exception e) {
            log.error("根据状态查询砍价商品列表失败", e);
            return Result.error("根据状态查询砍价商品列表失败: " + e.getMessage());
        }
    }
    
    /**
     * 健康检查
     */
    @GetMapping("/health")
    @Operation(summary = "健康检查", description = "检查砍价商品服务是否正常运行")
    public Result<String> health() {
        try {
            return  Result.success("砍价商品服务运行正常");
        } catch (Exception e) {
            log.error("健康检查失败", e);
            return  Result.error("健康检查失败: " + e.getMessage());
        }
    }
}